﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 学生管
{
    public partial class Form2 : Form
    {
        Dictionary<string, StuInfo> StuDic = new Dictionary<string, StuInfo>();
        private void FillGrid(Dictionary<string, StuInfo> dic)
        {
            if (dataGridViewStuInfo.ColumnCount == 0)
            {
                DataGridViewTextBoxColumn col_sno = new DataGridViewTextBoxColumn();
                col_sno.HeaderText = "学号";
                col_sno.DataPropertyName = "Sno";
                col_sno.Name = "sno";

                DataGridViewTextBoxColumn col_name = new DataGridViewTextBoxColumn();
                col_name.HeaderText = "姓名";
                col_name.DataPropertyName = "Name";
                col_name.Name = "name";

                DataGridViewTextBoxColumn col_class = new DataGridViewTextBoxColumn();
                col_class.HeaderText = "班级";
                col_class.DataPropertyName = "SClass";
                col_class.Name = "class";

                DataGridViewTextBoxColumn col_tele = new DataGridViewTextBoxColumn();
                col_tele.HeaderText = "性别";
                col_tele.DataPropertyName = "Tele";
                col_tele.Name = "tele";

                dataGridViewStuInfo.Columns.Add(col_sno);
                dataGridViewStuInfo.Columns.Add(col_name);
                dataGridViewStuInfo.Columns.Add(col_class);
                dataGridViewStuInfo.Columns.Add(col_tele);
            }
            BindingSource bs = new BindingSource();
            bs.DataSource = dic.Values;
            dataGridViewStuInfo.DataSource = bs;
        }
        public Form2()
        {
            InitializeComponent();
            this.Text = "学生信息管理系统";
  
              StuInfo zhang = new StuInfo("001", "张三", "1601", "男");
              StuInfo luo = new StuInfo("002", "罗辑", "1503", "男");
              StuInfo sun = new StuInfo("003", "孙雪", "1704", "女");
              StuInfo wang = new StuInfo("004", "王莱", "1605", "女");
  
              StuDic.Add(zhang.Sno, zhang);
              StuDic.Add(luo.Sno, luo);
              StuDic.Add(sun.Sno, sun);
              StuDic.Add(wang.Sno, wang);
  
              FillGrid(StuDic);
        }

        private void ButtonDele_Click(object sender, EventArgs e)
        {
            if (!StuDic.ContainsKey(Lebel1.Text))
            {
                if (!StuDic.ContainsKey(textBoxQuery.Text))
                {
                    MessageBox.Show("您要删除的元素不存在！", "错误",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                StuDic.Remove(textBoxQuery.Text);
                FillGrid(StuDic);
            }
        }

        private void ButtonEdit_Click(object sender, EventArgs e)
        {
            if (!StuDic.ContainsKey(textBoxQuery.Text))
            {
                MessageBox.Show("您要修改的数据不存在！", "错误",
                     MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            textBoxStuNo.Enabled = false;

            StuInfo s = StuDic[textBoxQuery.Text];

            textBoxName.Text = s.Name;
            textBoxClass.Text = s.SClass;
            textBoxStuNo.Text = s.Sno;
            textBoxTele.Text = s.Tele; if (!StuDic.ContainsKey(textBoxQuery.Text))
            {
                MessageBox.Show("您要修改的数据不存在！", "错误",
                     MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            textBoxStuNo.Enabled = false;


            textBoxName.Text = s.Name;
            textBoxClass.Text = s.SClass;
            textBoxStuNo.Text = s.Sno;
            textBoxTele.Text = s.Tele;
        }

        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            textBoxStuNo.Text = "";
            textBoxName.Text = "";
            textBoxClass.Text = "";
            textBoxTele.Text = "";
            textBoxStuNo.Enabled = true;  
        }

        private void ButtonOK_Click(object sender, EventArgs e)
        {
            if (textBoxStuNo.Enabled)
            {
                if (StuDic.ContainsKey(textBoxStuNo.Text))
                {
                    MessageBox.Show("学号已存在！", "警告",
                         MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (textBoxStuNo.Text == "" || textBoxName.Text == ""
                    || textBoxClass.Text == "" || textBoxTele.Text == "")
                {
                    MessageBox.Show("请将信息填写完整！", "警告",
                         MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                StuInfo s = new StuInfo(textBoxStuNo.Text, textBoxName.Text,
                    textBoxClass.Text, textBoxTele.Text);
                StuDic.Add(s.Sno, s);
            }
            else
            {
                if (textBoxName.Text == "" || textBoxClass.Text == "" || textBoxTele.Text == "")
                {
                    MessageBox.Show("请将信息填写完整！", "警告",
                         MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                StuDic.Remove(textBoxStuNo.Text);

                StuInfo s = new StuInfo(textBoxStuNo.Text, textBoxName.Text,
                    textBoxClass.Text, textBoxTele.Text);
                StuDic.Add(s.Sno, s);
            }

            FillGrid(StuDic);

            textBoxStuNo.Text = "";
            textBoxName.Text = "";
            textBoxClass.Text = "";
            textBoxTele.Text = "";
        }

        private void ButtonCel_Click(object sender, EventArgs e)
        {
            textBoxStuNo.Text = "";
            textBoxName.Text = "";
            textBoxClass.Text = "";
            textBoxTele.Text = "";
        }

       
    }
}
